package homeWork;

import java.util.Scanner;

public class RepayMentOS {

    //    贷款金额
    private double rePayMoney;
    //    还款时间
    private double rePayMonth;
    //    月利率
    private double interest_tate;

    Scanner sc = new Scanner(System.in);

    double getUserInput(String msg) {
        double userInput;
        while (true) {
            try {
                System.out.println(msg);
//                sc.nextLine();
                userInput = sc.nextDouble();
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误! 请重新输入。");
                sc.nextLine();
            }
        }
        return userInput;
    }

    public RepayMentOS() {

        this.rePayMoney = getUserInput("请输入您的贷款总金额：");
        this.rePayMonth = getUserInput("请输入您还款的月份：");
        this.interest_tate = getUserInput("请输入您的贷款月利率：");

        System.out.println("您的贷款总金额：" + this.rePayMoney + "  月利率: " + this.interest_tate + "   分" + this.rePayMonth + " 个月还清。");
    }

    void start() {
        double repaymoneyMonth;
        double monthInterestTate;
        double repaymoneyMonthTotal;
//           本金
        repaymoneyMonth = Math.round(this.rePayMoney / this.rePayMonth);
        for (int i = 1; i <= this.rePayMonth; i++) {
//            利息
            monthInterestTate = Math.round(this.rePayMoney * this.interest_tate / 100);
//          还款额
            repaymoneyMonthTotal = repaymoneyMonth + monthInterestTate;
            System.out.println("第" + i + "期共需还款：" + repaymoneyMonthTotal + "  ，详情如下：");
            System.out.println("本金: " + repaymoneyMonth + ",利息: " + monthInterestTate + "  合计：" + repaymoneyMonthTotal);
            this.rePayMoney -= repaymoneyMonth;
        }
    }

    public static void main(String[] args) {
        RepayMentOS rmo = new RepayMentOS();
        rmo.start();
    }
}
